//
//  AppDelegate.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseClassNavigationController.h"
#import "ContainerViewController.h"
#import "BaseClassNavgationBar.h"
#import "DrawerViewController.h"
#import <iflyMSC/iflyMSC.h>
#import "controlView.h"
#import "PlayerViewController.h"
#import "EAIntroView.h"

@interface AppDelegate ()<changeWindowWithContain, changeWindowWithDrawer, controlViewDetegate>

@property (nonatomic, retain) UIWindow *windowDrawer;
@property (nonatomic, retain) controlView *controllerView;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_windowDrawer release];
    [_controllerView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeControllerView" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeControlView" object:nil];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    NSString *string = @"appid=565453bc";
    [IFlySpeechUtility createUtility:string];
    
    self.windowDrawer = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.windowDrawer.backgroundColor = [UIColor whiteColor];
    [self.windowDrawer makeKeyAndVisible];
    [_windowDrawer release];
    
    DrawerViewController *drawerViewController = [DrawerViewController new];
    self.windowDrawer.rootViewController = drawerViewController;
    drawerViewController.delegate = self;
    [drawerViewController release];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    ContainerViewController *containerViewController = [ContainerViewController new];
    BaseClassNavigationController *naviagtionController = [[BaseClassNavigationController alloc] initWithRootViewController:containerViewController];
    containerViewController.delegate = self;
    [naviagtionController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsDefault];
    self.window.rootViewController = naviagtionController;
    [naviagtionController release];
    [containerViewController release];
    
    [self.window.subviews[self.window.subviews.count - 1] addSubview:self.controllerView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeControllerView) name:@"changeControllerView" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeControlView) name:@"changeControlView" object:nil];
    return YES;
}
- (void)changeControllerView
{
    [UIView animateWithDuration:0.4 animations:^{
        self.controllerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, 375, 50);
    }];
}
- (void)changeControlView
{
    [UIView animateWithDuration:0.4 animations:^{
        self.controllerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, 375, 50);
    }];
    [self.controllerView animation];
}

- (void)ChangeWindowInAppDelegate
{
    [UIView animateWithDuration:0.3 animations:^{
        self.window.frame = CGRectMake(300, 0, 375, 667);
    }];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 300, 567);
    button.tag = 1304;
    [self.window addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(UIButton *)sender
{
    UIButton *button = (UIButton *)[self.window viewWithTag:1304];
    [button removeFromSuperview];
    [UIView animateWithDuration:0.3 animations:^{
        self.window.frame = [UIScreen mainScreen].bounds;
    }];
}
- (controlView *)controllerView
{
    if (nil == _controllerView)
    {
        self.controllerView = [[controlView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 80, 375, 80)];
        self.controllerView.delegate = self;
        [self.controllerView.layer setShadowColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0].CGColor];
        self.controllerView.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
        [self.controllerView.layer setShadowOpacity:.3f];
        [_controllerView release];
    }
    return _controllerView;
}
- (void)changeWindowWithDrawerViewController
{
    UIButton *button = (UIButton *)[self.window viewWithTag:1304];
    [button removeFromSuperview];
    [UIView animateWithDuration:0.3 animations:^{
        self.window.frame = [UIScreen mainScreen].bounds;
    }];
}
- (void)toPlayerView
{
    PlayerViewController *playerViewController = [PlayerViewController sharePlayerViewController];
    BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:playerViewController];
    [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
    [self.window.rootViewController presentViewController:navgationController animated:YES completion:^{
        
    }];
    [navgationController release];
}

- (void)showIntroWithCrossDissolve{
    EAIntroPage *page1 = [EAIntroPage page];
    page1.bgImage = [UIImage imageNamed:@"01"];
    
    EAIntroPage *page2 = [EAIntroPage page];
    page2.bgImage = [UIImage imageNamed:@"02"];
    
    EAIntroPage *page3 = [EAIntroPage page];
    page3.bgImage = [UIImage imageNamed:@"03"];
    
    EAIntroView *intro = [[EAIntroView alloc] initWithFrame:self.window.bounds andPages:@[page1,page2,page3]];
    //    [intro setDelegate:self];
    [intro showInView:self.window animateDuration:0.5];
    [intro release];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    //判断是不是第一次加载
    NSString *firstLoad = @"FIRSTLOAD";
    if (![[NSUserDefaults standardUserDefaults] objectForKey:firstLoad]) {
        [self showIntroWithCrossDissolve];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:firstLoad];
    }
    
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
